﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MedicineTracker.Model;

namespace MedicineTracker.ViewModel
{
    class DisposalsViewModel: Database, INotifyPropertyChanged
    {
        private ObservableCollection<Disposal> _disposals;

        public ObservableCollection<Disposal> Disposals
        {
            get { return _disposals; }
        }

        public DisposalsViewModel()
        {
            _disposals = new ObservableCollection<Disposal>();
            using (Connection)
            {
                try
                {
                    Connection.Open();
                    Command.CommandText = "SELECT dis.id, dis.date_disposed, dis.quantity_disposed AS qty, dis.route, dis.purchase AS pur_id, pur.batch_no, pur.medicine_id AS med_id, med.name AS med  FROM disposals dis INNER JOIN purchases pur ON dis.purchase = pur.id INNER JOIN medicines med ON pur.medicine_id = med.id";
                    Command.Connection = Connection;
                    DataReader = Command.ExecuteReader();
                    while (DataReader.Read())
                    {
                        _disposals.Add(new Disposal
                        {
                            ID = Convert.ToInt32(DataReader["id"]),
                            Batch = (string)DataReader["batch"],
                            DateDisposed = (DateTime)DataReader["date_disposed"],
                            PurcahseID = Convert.ToInt32(DataReader["pur_id"]),
                            QuatityDisposed = Convert.ToInt32(DataReader["qty"]),
                            Route = (string)DataReader["route"]
                        });
                    }
                }
                catch
                {
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
